<?php
class ModelCatalogMovelog extends Model {
	/**
	 * 新建一条转移日志，一般为当用户提交转移时
	 * @param array $data
	 * @return movelog_id
	 */
	public function addMovelog($data) {
		$sql = "INSERT INTO  ". DB_PREFIX ."movelog SET ".
			"product_id = " . (int)$data['product_id'] .",".
			"from_customer_id = " . (int)$data['from_customer_id'] .",".
			"to_customer_id = " . (int)$data['to_customer_id'] .",".
			"from_keeplace_id = " . (int)$data['from_keeplace_id'] .",".
			"status = 2".",".
			"move_time = NOW()";
		
		$this->db->query($sql);
		$movelog_id = $this->db->getLastId();
		
		$sql = "UPDATE ". DB_PREFIX ."product SET movelog_id = '" . (int)$movelog_id."' WHERE product_id = '".(int)$data['product_id']."'";
		$this->db->query($sql);
		
		return $movelog_id;
	}
	
	/**
	 * 按id得到movelog
	 * @param int $movelog_id
	 */
	public function getMovelog($movelog_id){
		$sql = "SELECT * FROM ". DB_PREFIX."movelog WHERE movelog_id = '".(int)$movelog_id."'";
		
		$query = $this->db->query($sql);
		return $query->row;
	}
	
	/**
	 * 根据用户和状态得到maglog列表
	 * @param unknown_type $customer_id
	 * @param unknown_type $status
	 */
	public function getMovelogsByCustomer($customer_id,$status){
		$sql = 
			"SELECT * FROM ".DB_PREFIX."movelog ml ".
			"LEFT JOIN ".DB_PREFIX."customer c ON ml.from_customer_id = c.customer_id ".
			"LEFT JOIN ".DB_PREFIX."keeplace kp ON ml.from_keeplace_id = kp.keeplace_id ".
			"WHERE ml.to_customer_id = '".(int)$customer_id."' AND ml.status = '".(int)$status."' ".
			"ORDER BY ml.move_time DESC";
		
		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	/**
	 * 改变movelog状态
	 * @param unknown_type $movelog_id 
	 * @param unknown_type $status 0,1,2
	 */
	public function setMovelogStatus($movelog_id, $status){
		$this->db->query("UPDATE " . DB_PREFIX . "movelog SET status = '".(int)$status."' WHERE movelog_id = '" . (int)$movelog_id . "'");
	}
	
	public function setMovelogKeeplace($movelog_id, $keeplace_id){
		$this->db->query("UPDATE " . DB_PREFIX . "movelog SET to_keeplace_id = '".(int)$keeplace_id."' WHERE movelog_id = '" . (int)$movelog_id . "'");
	}
}